import 'package:flutter/material.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';

class FunnelChart extends CustomPainter {
  Paint mPaint = Paint()..color = Colors.blue;
  Paint mTextPaint = Paint()
    ..color = Colors.white
    ..style = PaintingStyle.fill;
  final double height;
  final double margin;
  final double between;
  final double squareWidth;
  final List? dataList;
  final Color paintColor;

  FunnelChart(
      {this.height = 56,
      this.between = 20,
      this.margin = 50,
      this.squareWidth = 4,
      this.paintColor = WColors.primaryColor,
      this.dataList});

  @override
  void paint(Canvas canvas, Size size) {
    drawTrapezium(canvas, size);
    drawTrapText(canvas, size);
    drawSquare(canvas, size);
    drawSquareText(canvas, size);
  }

  //绘制梯形
  void drawTrapezium(Canvas canvas, Size size) {
    int length = dataList!.length;

    for (int i = 0; i < length; i++) {
      Offset topLeft = Offset(between.px * i + margin.px, i * (height + 5).px,);
      Offset topRight =
          Offset(size.width - (between.px * i) - margin.px, i * (height + 5).px);
      Offset bottomLeft =
          Offset(between.px * (i + 1) + margin.px, i * (height + 5).px + height.px);
      Offset bottomRight = Offset(size.width - between.px * (i + 1) - margin.px,
          i * (height + 5).px + height.px);
      Path path = Path();
      path.moveTo(topLeft.dx, topLeft.dy);
      path.lineTo(topRight.dx, topRight.dy);
      path.lineTo(bottomRight.dx, bottomRight.dy);
      path.lineTo(bottomLeft.dx, bottomLeft.dy);
      path.close();
      mPaint.color = paintColor.withOpacity((length - i) * 0.15);
      canvas.drawPath(path, mPaint);
    }
  }

  //绘制梯形中的文字
  void drawTrapText(Canvas canvas, Size size) {
    int length = dataList!.length;
    for (int i = 0; i < length; i++) {
      String text = dataList![i]["count"].toString();
      Size textSize = Size(size.width, (height.px + 5.px) * (2 * i + 1));
      final textSpan = TextSpan(
        text: text,
        style: TextStyle(
          color: Colors.white,
          fontSize: WDimens.tsXXLarge,
        ),
      );
      final textPainter = TextPainter(
        text: textSpan,
        textDirection: TextDirection.ltr,
      );
      textPainter.layout(
        minWidth: 0,
        maxWidth: textSize.width,
      );
      final Offset textOffset = Offset(
          textSize.width / 2 - textPainter.width / 2,
          textSize.height / 2 - textPainter.height / 2);


      textPainter.paint(canvas, textOffset);
    }

  }

  //绘制文案
  void drawSquare(Canvas canvas, Size size) {
    int length = dataList!.length;
    for (int i = 0; i < length; i++) {
      mPaint.color = paintColor.withOpacity((length - i) * 0.15);
      double x =  10.px + squareWidth.px / 2;
      double y = height * i + 10.px + squareWidth.px / 2;
      Offset center = Offset(x,y);

      Rect rect = Rect.fromCircle(center: center, radius: squareWidth.px);
      canvas.drawRect(rect, mPaint);
    }
  }

  //绘制矩形旁边的文字
  void drawSquareText(Canvas canvas, Size size) {
    int length = dataList!.length;
    for (int i = 0; i < length; i++) {
      String text = dataList![i]["label"];
      final textSpan = TextSpan(
        text: text,
        style: TextStyle(
          color: WColors.mainTextColor,
          fontSize: WDimens.tsSmall,
        ),
      );
      final textPainter = TextPainter(
        text: textSpan,
        textDirection: TextDirection.ltr,
      );
      textPainter.layout(minWidth: 0, maxWidth: 60.px);
      final Offset textOffset = Offset(20.px + squareWidth / 2,
          (10 + squareWidth / 2).px + height * i - textPainter.height / 2);
      textPainter.paint(canvas, textOffset);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }
}
